package com.iscas.green.httpclienttest.httpclient;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpMethod;
import org.apache.commons.httpclient.methods.GetMethod;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

@Component
public class HttpClient3Example {
    private static final Logger logger = LoggerFactory.getLogger(HttpClient3Example.class);

    private static final String URL = "http://database-test.chaos-test.svc.cluster.local:8080/api/test/clickhouse";

    public static String callService() {
        HttpClient client = new HttpClient();
        HttpMethod method = new GetMethod(URL);

        logger.info("Starting call to {}", URL); // 添加日志，记录请求开始

        try {
            client.executeMethod(method);
            String response = method.getResponseBodyAsString();
            logger.info("Received response: {}", response); // 添加日志，记录响应
            return response;
        } catch (Exception e) {
            logger.error("Error while calling service", e); // 添加日志，记录异常
            return "Error: " + e.getMessage();
        } finally {
            method.releaseConnection();
            logger.info("Released connection for {}", URL); // 添加日志，记录连接释放
        }
    }
}
